package com.cn.medicalcare.service.serviceimpl;

import com.cn.medicalcare.customtool.JedisUtil;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.customtool.SqlSessionFactoryTool;
import com.cn.medicalcare.mapper.CompanyIntroduceMapper;
import com.cn.medicalcare.mapper.NewsInfoMapper;
import com.cn.medicalcare.mapper.ProductInfoMapper;
import com.cn.medicalcare.pojo.CompanyIntroduce;
import com.cn.medicalcare.pojo.NewsAllInfo;
import com.cn.medicalcare.pojo.NewsInfo;
import com.cn.medicalcare.service.NewsInfoService;
import org.apache.commons.lang3.StringUtils;


public class NewsInfoServiceImpl implements NewsInfoService {

    public static final String NEWS_CACHE_KEY = "news_all_info_by_id_";

    //静态内部类单例模式
    private static class SingletonClassInstance {
        private static final NewsInfoServiceImpl instance = new NewsInfoServiceImpl();
    }

    private NewsInfoServiceImpl() {
    }

    public static NewsInfoServiceImpl getInstance() {
        return NewsInfoServiceImpl.SingletonClassInstance.instance;
    }

    @Override
    public NewsInfo selectNewsInfoByNews_id(int news_id) {
        return SqlSessionFactoryTool.DoMapper(NewsInfoMapper.class, m -> m.selectNewsInfoByNews_id(news_id));
    }


    @Override
    public boolean insertNewsInfo(NewsInfo newsInfo) {
        return SqlSessionFactoryTool.DoMapperAndCommit(NewsInfoMapper.class, m -> m.insertNewsInfo(newsInfo));
    }

    @Override
    public boolean updateNewsInfo(NewsInfo newsInfo) {
        boolean updateFlag = SqlSessionFactoryTool.DoMapperAndCommit(NewsInfoMapper.class, m -> m.updateNewsInfo(newsInfo));
        String cachekey = NEWS_CACHE_KEY + newsInfo.getNews_id();
        if (updateFlag) {
            JedisUtil.jedisDo(j -> j.del(cachekey));
        }
        return updateFlag;
    }

    @Override
    public NewsAllInfo selectNewsAllInfoById(int id) {
        NewsAllInfo newsAllInfo = null;
        String cachekey = NEWS_CACHE_KEY + id;
        try {
            String newsAllInfoJson = JedisUtil.jedisDo(j -> j.get(cachekey));
            if (StringUtils.isNotEmpty(newsAllInfoJson)) {
                newsAllInfo = JsonTool.toObject(newsAllInfoJson, NewsAllInfo.class);
                if (newsAllInfo != null) {
                    NewsInfo newsInfo = newsAllInfo.getNewsInfo();
                    newsInfo.setNumviews(newsInfo.getNumviews() + 1);
                    String new_newsAllInfoJson = JsonTool.toJson(newsAllInfo);
                    JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, new_newsAllInfoJson));
                }
            }
        } catch (Exception e) {
        }
        SqlSessionFactoryTool.DoMapperAndCommit(NewsInfoMapper.class, m -> m.updateNumviews(id));
        if (newsAllInfo == null) {
            newsAllInfo = SqlSessionFactoryTool.DoMapper(NewsInfoMapper.class, m -> m.selectNewsAllInfoById(id));
            try {
                String newsAllInfoJson = JsonTool.toJson(newsAllInfo);
                JedisUtil.jedisDo(j -> j.setex(cachekey, JedisUtil.CACHE_TIME, newsAllInfoJson));
            } catch (Exception e) {
            }
        }
        return newsAllInfo;
    }
}
